﻿using Siasun.AFC.RtPlatfromGui.IBll.IServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Siasun.AFC.RtPlatfromGui.IBll.Model;
using Siasun.AFC.RtPlatfromGui.Dao.Device;

namespace Siasun.AFC.RrPlatfromGui.Bll.DeviceDataServer
{
    /// <summary>
    /// 菜单数据服务工厂接口
    /// </summary>
    public class MenuDataServer : IMenuDataServer
    {
        /// <summary>
        /// 数据操作类
        /// </summary>
        private RtMenuDataDao menuDataDao = new RtMenuDataDao();

        /// <summary>
        /// 查询所有菜单
        /// </summary>
        /// <returns></returns>
        public List<MenuInfo> QueryAllMenuData()
        {
            List<MenuInfo> menuInfoList = new List<MenuInfo>();
            MenuInfo menuInfo = null;
            foreach (var item in menuDataDao.QueryAllMenuData())
            {
                menuInfo = MenuInfo.ConvertInfo(item);
                if(!string.IsNullOrEmpty(menuInfo.Id))
                    menuInfoList.Add(menuInfo);
            }
            return menuInfoList;
        }

        /// <summary>
        /// 查询单个菜单
        /// </summary>
        /// <param name="menuId"></param>
        public MenuInfo GetMenuData(string menuId)
        {
            return MenuInfo.ConvertInfo(menuDataDao.GetMenuDataById(menuId));
        }

        /// <summary>
        /// 插入菜单
        /// </summary>
        /// <param name="menuInfo"></param>
        public void InsertMenuData(MenuInfo menuInfo)
        {
            if (object.Equals(null, menuInfo))
            {
                //日志插入数据不完善
                return;
            } 

            menuDataDao.InsertData(MenuInfo.ConvertData<RtPlatfromGui.Dao.Define.RtMenuDataModel>(menuInfo));
        }

        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="menuInfo"></param>
        public void UpdateMenuData(MenuInfo menuInfo)
        {
            if (object.Equals(null, menuInfo) || string.IsNullOrEmpty(menuInfo.Id))
            {
                //日志插入数据不完善
                return;
            }

            menuDataDao.UpdateData(MenuInfo.ConvertData<RtPlatfromGui.Dao.Define.RtMenuDataModel>(menuInfo));
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="menuId"></param>
        public void DeleteMenuData(string menuId)
        {
            menuDataDao.DeleteData(menuId);
        }

        /// <summary>
        /// 删除菜单及其子节点
        /// </summary>
        /// <param name="parentId"></param>
        public void DeleteMenuDataAndAllChild(string parentId)
        {
            try
            {
                //查询全部菜单
                List<MenuInfo> tempMenuInfoList = QueryAllMenuData();
                tempMenuInfoList = tempMenuInfoList.Where(t => t.ParentMenuId == parentId).ToList();
                //过滤父菜单节点
                List<string> childIdList = new List<string>();
                foreach (var item in tempMenuInfoList)
                {
                    childIdList.Add(item.Id);
                }
                //删除所有子节点
                menuDataDao.DeleteData(childIdList);
                //删除父菜单节点
                menuDataDao.DeleteData(parentId);
            }
            catch
            {
                //记录日志
            }
        }
    }
}
